#include "../ModelEditorConfig.h"
#include "../Model/MD2.h"
#include "../Model/ms3d.h"
#include "ResourceDirComponent.h"
#include "MainFrame.h"

bool IsTextureFile(const File &file)
{
	if( file.getFileExtension().equalsIgnoreCase(".png") ||
		file.getFileExtension().equalsIgnoreCase(".bmp") ||
		file.getFileExtension().equalsIgnoreCase(".jpg") ||
		file.getFileExtension().equalsIgnoreCase(".tga") ||
		file.getFileExtension().equalsIgnoreCase(".dds") )
	{
		return true;
	}
	return false;
}

bool ModelAndTextureFileFilter::isFileSuitable(const File &file) const
{
	if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD2 &&
		(file.getFileExtension().equalsIgnoreCase(".md2")) )
		return true;
	if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD3 &&
		(file.getFileExtension().equalsIgnoreCase(".md3")) )
		return true;
	if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMS3D &&
		(file.getFileExtension().equalsIgnoreCase(".ms3d")) )
		return true;

	if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowPicture )
	{
		return IsTextureFile(file);
	}

	return false;
}

//==============================================================================


void ResourceDirComponent::fileDoubleClicked(const File &file)
{
	if( !file.isDirectory() )
	{
		if( file.getFileExtension().equalsIgnoreCase(".md2") )
		{
			CModelEditorConfig::GetInstance()->CloseScene();

			g_pCurrentModelFile = new CMd2(file.getFullPathName().getCharPointer());
			CMd2 *pMD2File = static_cast<CMd2*>(g_pCurrentModelFile);

			if( pMD2File->GetTextureNum() == 0 )
			{
				String TexFileName = file.getParentDirectory().getFullPathName() + String("\\") + file.getFileNameWithoutExtension() + String(".bmp");
				if( !File(TexFileName).exists() )
					TexFileName = file.getParentDirectory().getFullPathName() + String("\\") + file.getFileNameWithoutExtension() + String(".jpg");
				if( !File(TexFileName).exists() )
					TexFileName = file.getParentDirectory().getFullPathName() + String("\\") + file.getFileNameWithoutExtension() + String(".png");
				if( !File(TexFileName).exists() )
					TexFileName = file.getParentDirectory().getFullPathName() + String("\\") + file.getFileNameWithoutExtension() + String(".tga");
				if( !File(TexFileName).exists() )
					TexFileName = file.getParentDirectory().getFullPathName() + String("\\") + file.getFileNameWithoutExtension() + String(".dds");

				g_pDevice->GetSkinManager()->AddDeferedTexture(g_DefaultSkin, TexFileName.toWideCharPointer(), 0);
				pMD2File->SetCustomTexture(true);
			}
			else
			{
				pMD2File->SetTexture(g_DefaultSkin, 0);
			}
			pMD2File->SetScaling(10.0f);
			pMD2File->CreateStaticMesh(g_DefaultSkin);
			

			pMainFrame->m_pD3DCanvas->m_ControlPanel->ResetUI();
		}
		else if( file.getFileExtension().equalsIgnoreCase(".ms3d") )
		{
			CModelEditorConfig::GetInstance()->CloseScene();

			g_pCurrentModelFile = new CMs3d(file.getFullPathName().getCharPointer());
			CMs3d *pMS3DFile = static_cast<CMs3d*>(g_pCurrentModelFile);

			pMS3DFile->BindTexture();
			pMS3DFile->SetScaling(10.0f);
			pMS3DFile->CreateStaticMesh(g_DefaultSkin);

			pMainFrame->m_pD3DCanvas->m_ControlPanel->ResetUI();
		}
		else if( IsTextureFile(file) )
		{
			AlertWindow w( "", file.getFullPathName(), AlertWindow::InfoIcon );

			TextureViewDlg = new TextureViewComponent(file.getFullPathName().toWideCharPointer());
			w.addTextBlock( String("Texture Size : ") + String(TextureViewDlg->GetImageWidth()) + String(L" X ") + String(TextureViewDlg->GetImageHeight()) +
				String((TextureViewDlg->GetColorBits() == 32) ? L"  A8R8G8B8  " : L"  R8G8B8  " ) +
				String((TextureViewDlg->GetImageScale() < 1.0f) ? TextureViewDlg->GetImageScale()*100 : 100) + String(L"%") );
			w.addCustomComponent( TextureViewDlg );


			w.addButton(L"ok", 1, KeyPress(KeyPress::returnKey, 0, 0));

			// picked 'ok'
			if( w.runModalLoop() != 0 ) 
			{
				TextureViewDlg = NULL;
			}
		}
		else
		{
			AlertWindow::showMessageBox(AlertWindow::WarningIcon, L"Not supported file ", file.getFullPathName());
		}
	}
}